﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BluePortal.Medias.Core;
using BluePortal.Framework;

namespace BluePortal.QuikStart.UserControls
{
    public partial class VideoList : System.Web.UI.UserControl
    {
        public Enum<VideoListMode> mode = VideoListMode.Browse;

        public string ModeStr
        {
            get { return mode.ToString(); }
            set { mode = value; } 
        }
        public Guid videoSelected = Guid.Empty;
        private IEnumerable videosList;
        public IEnumerable VideosList
        {
            get { return videosList; }
            set { videosList = value; }
        }

        private Guid playlistId;

        public Guid PlaylistId
        {
            get { return playlistId; }
            set { playlistId = value; }
        }

        public bool ImageAddPlayListVisibility
        {
            get
            {
                return ((Page.User.Identity.IsAuthenticated) && (mode.Contains(VideoListMode.Browse)));
            }
        }

        public bool ImageAddFavoriteVisibility
        {
            get {
                return ((Page.User.Identity.IsAuthenticated) && (mode.Contains(VideoListMode.Browse)));
            }

        }

        public bool ImageRemovePlaylistVisibility
        {
            get
            {
                return mode.Contains(VideoListMode.RemovePlaylist);
            }

        }
        public bool ImageRemoveFavoriteVisibility
        {
            get
            {
                return mode.Contains(VideoListMode.RemoveFav);
            }

        }
        public bool VideoRatingVisibility
        {
            get {
                return mode.Contains(VideoListMode.Rating);
            }
        }
        public bool AdvancedDescriptionVisibility
        {
            get {
                return mode.Contains(VideoListMode.AdvancedDescription);
            }

        }
        public bool StatusVisibility
        {
            get {
                return mode.Contains(VideoListMode.Status);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ObjectDataSource2.SelectParameters["loginName"].DefaultValue = Page.User.Identity.Name;
                if (videosList != null)
                {
                    IEnumerable<Media> medias = videosList.OfType<Media>();
                    medias = medias.ToList();
                    Repeater1.DataSource = medias;
                    Repeater1.DataBind(); 
                }
            }
            if (((BluePortalMaster)Page.Master).scriptManager.IsInAsyncPostBack)
            {
                ddlPlaylist.DataBind();
            }

            if (mode.Contains(VideoListMode.RemoveFav) || mode.Contains(VideoListMode.RemovePlaylist))
            {
                upPopup.Visible = false;
            }

        }

        public override void DataBind()
        {
            base.DataBind();
            if ((videosList != null) && (Repeater1.DataSource == null))
            {
                if (playlistId != Guid.Empty)
                    ViewState["playlistId"] = playlistId;
                Repeater1.DataSource = videosList;
                Repeater1.DataBind();
            }
          
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "selectVideo":
                    Response.Redirect("Details.aspx?vid=" + e.CommandArgument);
                    break;
                case "editVideo":
                    Response.Redirect("EditVideo.aspx?vid=" + e.CommandArgument);
                    break;
                case "deleteFavorite":
                    //@todo map favorite to playlist
                    //bcvideo.Delete(new BluePortal.Core.BusinessObjects.Favorite() { LoginName = Context.User.Identity.Name, VID = new Guid(e.CommandArgument.ToString()) });
                    //Page.DataBind();
                    break;
                case "deletePlaylist":
                    new BCPlaylist().Remove((Guid)ViewState["playlistId"], new Guid(e.CommandArgument.ToString()));
                    Page.DataBind();
                    break;
                case "selectOwner":
                    Response.Redirect("Browse.aspx?user=" + e.CommandArgument);
                    break;
            }
        }
    }

    public enum VideoListMode : int
    {
        Browse = 1,
        RemoveFav = 2,
        RemovePlaylist = 4,
        StandardDescription = 8,
        AdvancedDescription = 16,
        Rating = 32,
        Status = 64,
        Home = 41,
        Default = 57,
        DefaultPlusStatus = 121,
        Full = 127
    }
}